#coding:utf-8
from django.shortcuts import render,render_to_response
from django.http import HttpResponseRedirect,HttpResponse
from django.template import RequestContext
from django.core.paginator import Paginator
from snippets.models import Snippet
from blogs.models import Blog,Term
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.decorators import api_view, renderer_classes
from snippets.serializers import SnippetSerializer
# Create your views here.
# 
def required_log(view):
    def panduan(request,*args,**kwargs):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/urlconf/failed/')
        return view(request,*args,**kwargs)
    return panduan

def index(request):
    return render_to_response('snippet_index.html')

def list(request,page,templateName,objType):
    try:
        objs = objType.objects.all()
        paginator_blog = Paginator(objs,3)
        num_pages = paginator_blog.num_pages
        next_page = 0
        prevous_page = 0
        if page != '':
            if int(page) < 1:
                page = 1
            if int(page) > num_pages:
                page = num_pages
            page_blog = paginator_blog.page(int(page))
            if page_blog.has_next():
                next_page = page_blog.next_page_number()
            if page_blog.has_previous():
                prevous_page = page_blog.previous_page_number()
            return render_to_response(templateName,{'objs':page_blog.object_list,'page_range':paginator_blog.page_range,'next_page':next_page,'prevous_page':prevous_page,'current_page':int(page)})
        else:
            page_blog = paginator_blog.page(1)
            return render_to_response(templateName,{'objs':page_blog.object_list,'page_range':paginator_blog.page_range,'next_page':next_page,'prevous_page':prevous_page,'current_page':page})
            return Response({'objs':page_blog.object_list,'page_range':paginator_blog.page_range,'next_page':next_page,'prevous_page':prevous_page,'current_page':page})
    except Exception,e:
        return HttpResponseRedirect('/urlconf/index/')

@api_view(['get'])
@renderer_classes((JSONRenderer,))
def snippets_list(request):
   from dss.Serializer import serializer
   snippets = Snippet.objects.all()
   #serializer = SnippetSerializer(snippets)
   data = serializer(snippets)
   return Response(data)

def detail(request,id):
    id = int(id)
    snippet = Snippet.objects.get(id=id)
    snippet_prevous_id = 0
    snippet_next_id = 0
    snippet_prevous = Blog.objects.raw("select * from blog where id<%s order by id desc limit 0,1",[id])
    snippet_next = Blog.objects.raw("select * from blog where id>%s order by id asc limit 0,1",[id])
    flag = False
    flag_next = False
    for snippet_p in snippet_prevous:
        flag = True
    for snippet_n in snippet_next:
        flag_next = True
    if flag:
        snippet_prevous_id = snippet_prevous[0].id
    if flag_next:
        snippet_next_id = snippet_next[0].id
    return render_to_response('snippet_detail.html',{'snippet':snippet,
        'snippet_prevous_id':snippet_prevous_id,'snippet_next_id':snippet_next_id})

def success(request):
    return render_to_response('success.html')

